package com.netcracker.educenter.filestorage.datasource.dao.user;

import com.netcracker.educenter.filestorage.datasource.connection.ConnectionPool;
import com.netcracker.educenter.filestorage.datasource.dao.UserAccess;
import com.netcracker.educenter.filestorage.datasource.entity.User;
import org.apache.log4j.Logger;

import java.sql.*;

public class UserDAO implements UserAccess {

    private static final Logger log = Logger.getLogger(UserDAO.class);

    private static final String SELECT_USERS_QUERY = "SELECT * FROM users";
    private static final String SELECT_USER_WITH_LOGIN_QUERY = "SELECT * FROM users WHERE login = ?";
    private static final String INSERT_NEW_USER_QUERY = "INSERT INTO users (login, password) VALUES (?, ?)";

    @Override
    public boolean userIsLogged(String login, String password) {

        if (login == null || password == null) return false;

        Connection connection = ConnectionPool.getConnectionPool().getConnection();

        try (Statement statement = connection.createStatement()) {

            ResultSet resultSet = statement.executeQuery(SELECT_USERS_QUERY);

            while (resultSet.next()) {

                if (login.equals(resultSet.getString("login"))){

                    return password.equals(resultSet.getString("password"));
                }
            }

        } catch (SQLException e) {
            log.error("Cannot get users list", e);
        }

        ConnectionPool.getConnectionPool().releaseConnection(connection);

        return false;
    }

    @Override
    public boolean userExist(String login) {

        if (login == null) return false;

        Connection connection = ConnectionPool.getConnectionPool().getConnection();

        try (PreparedStatement preparedStatement = connection.prepareStatement(SELECT_USER_WITH_LOGIN_QUERY)) {

            preparedStatement.setString(1, login);
            ResultSet resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) return true;

        } catch (SQLException e) {
            log.error("Cannot get users list", e);
        }

        ConnectionPool.getConnectionPool().releaseConnection(connection);
        return false;
    }

    @Override
    public boolean add(User user) {

        if (user == null || user.getLogin() == null || user.getPassword() == null) return false;

        Connection connection = ConnectionPool.getConnectionPool().getConnection();

        try(PreparedStatement preparedStatement = connection.prepareStatement(INSERT_NEW_USER_QUERY)) {

            preparedStatement.setString(1, user.getLogin());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.executeUpdate();

        } catch (SQLException e) {
            log.error(String.format("Cannot add user %s in database", user.getLogin()), e);
            return false;
        }

        ConnectionPool.getConnectionPool().releaseConnection(connection);
        return true;
    }
}
